/**
 * Created with CLion.
 * Description: 
 * User: loong
 * Date: 2023/12/19
 * Time: 21:06
 */

#include "Doc.h"
#include "NodeImpl.h"

namespace CES::Config {
    DocSPtr Doc::FromFile(const std::string & source) {
        xml_document doc;
        doc.load_file(source.c_str());

        auto root = NodeImpl::ToNode(doc.first_child());

        return std::static_pointer_cast<Doc>(root);
    }

    Doc::Doc(std::string _key, std::string _value)
        : Node(_key,_value)
    {
    }

    Doc::Doc()
        : Node()
    {
    }

    bool Doc::ToFile(const std::string &_file, const std::string &_indent)
    {
        auto doc = NodeImpl::ToXmlNode(shared_from_this());

        return doc.save_file(_file.c_str(), _indent.c_str());
    }

    NodeList Doc::Query(const std::string & _path)
    {
        xpath_query          query(_path.c_str());

        auto xml_node = NodeImpl::ToXmlNode(shared_from_this());
        auto query_ret = xml_node.select_nodes(query);

        NodeList ret;
        for(const auto &item : query_ret) {
            auto ss = item.node().child_value();
            ret.emplace_back(NodeImpl::ToNode(item.node()));
        }

        return ret;
    }

}// namespace CES::Config